package thread;

public class StopDemo {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                int count = 1;
                //isInterrupted()测试此线程是否已中断。线程的中断状态不受此方法的影响。
                while(!Thread.currentThread().isInterrupted()) {
                    System.out.println("没有中断");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    count++;
                    if (count > 5){
                        //线程中断
                        Thread.currentThread().interrupt();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable,"线程A");
        thread.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //线程中断
//        thread.stop();
    }
}
